package cc.chinagps.gboss.common.util;

import lombok.extern.slf4j.Slf4j;

import java.io.File;
import java.io.FileInputStream;
import java.util.Properties;

@Slf4j
public class PropertiesUtil {
    public static Properties property;
    public static final String CONF_FILE_NAME = "gboss.properties";
    public static final String CONF_FILE_PATH = "conf/";
    private static long lastModified = 0L;
    private static File confFile;

    static {
        initConfigMap();
    }

    public synchronized static void initConfigMap() {
        try {
            confFile = new File(CONF_FILE_PATH + CONF_FILE_NAME);
            lastModified = confFile.lastModified();
            property = new Properties();
            property.load(new FileInputStream(confFile));
        } catch (Exception e) {
            log.error(e.getMessage(), e);
        }
    }

    public static String getString(String key) {
        synchronized (property) {
            Object obj = property.get(key);
            if (obj == null) {
                return null;
            } else {
                return (String) obj;
            }
        }
    }

    public static String getString(String key, String defaultVal) {
        String str = getString(key);
        return str == null ? defaultVal : str;
    }

    public static Integer getInteger(String key) {
        String str = getString(key);
        if ((str != null) && (!"".equals(str))) {
            try {
                return Integer.valueOf(str);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public static Integer getInteger(String key, int defaultVal) {
        Integer val = getInteger(key);
        if (val == null) {
            val = defaultVal;
        }
        return val;
    }

    public static Long getLong(String key) {
        String str = getString(key);
        if ((str != null) && (!"".equals(str))) {
            try {
                return Long.valueOf(str);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public static Double getDouble(String key) {
        String str = getString(key);
        if ((str != null) && (!"".equals(str))) {
            try {
                return Double.valueOf(str);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public static Boolean getBoolean(String key) {
        String str = getString(key);
        if ((str != null) && (!"".equals(str))) {
            try {
                return Boolean.valueOf(str);
            } catch (NumberFormatException e) {
                log.error(e.getMessage(), e);
            }
        }
        return null;
    }

    public static Boolean getBoolean(String key, boolean defaultVal) {
        Boolean val = getBoolean(key);
        if (val == null) {
            val = defaultVal;
        }
        return val;
    }

    public static void main(String[] args) {

    }

}